package com.ColonyCount.Activity;

import com.ColonyCount.ColonyCountContext;
import com.ColonyCount.ColonyCountHeap;
import com.ColonyCount.OnThresholdChangedListener;
import com.ColonyCount.R;
import com.ColonyCount.Image.Histogram;
import com.ColonyCount.Image.Image32;
import com.ColonyCount.Image.Image8;
import com.ColonyCount.R.id;
import com.ColonyCount.R.layout;
import com.ColonyCount.View.HistogramView;
import com.ColonyCount.View.OverlayView;
import com.ColonyCount.View.ThresholdView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class ThresholdActivity extends Activity implements OnThresholdChangedListener, DialogInterface.OnCancelListener {

	ColonyCountContext cccontext;
	ThresholdView thresholdView;
	HistogramView histogramView;
	
    final Handler mHandler = new Handler();
    Thread workerThread = null;

    ProgressDialog dialog;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
    	ColonyCountContext.dumpActivityEvent(this, "ThresholdActivity.onCreate() start");
    	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.threshold);
		
		cccontext = ColonyCountContext.getSingleton();
		
		// Setup histogram
		histogramView = (HistogramView) findViewById(R.id.histogramView);
		histogramView.registerThresholdChangedListener(this);
		
		// Setup threshold view
		thresholdView = (ThresholdView) findViewById(R.id.imagePreview);

		Button buttonThreshold = (Button) findViewById(R.id.buttonNext);
		buttonThreshold.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				startWorkerThread();
			}
		});
		
    	ColonyCountContext.dumpActivityEvent(this, "ThresholdActivity.onCreate() end");
	}
	
    @Override
	protected void onResume() {
    	ColonyCountContext.dumpActivityEvent(this, "ThresholdActivity.onResume() start");
    	super.onResume();
    	
    	setupViews();
		
    	ColonyCountContext.dumpActivityEvent(this, "ThresholdActivity.onResume() end");
    }
	
    @Override
	protected void onPause() {
    	ColonyCountContext.dumpActivityEvent(this, "ThresholdActivity.onPause() start");
    	super.onPause();
		
//		stopWorkerThread();
		if(dialog != null)
		{
			dialog.dismiss();
		}
		removeHeapReferences();
		
    	ColonyCountContext.dumpActivityEvent(this, "ThresholdActivity.onPause() end");
	}
    
    void setupViews()
    {
    	histogramView.setThreshold(cccontext.getThreshold());
        histogramView.setHistogram(ColonyCountHeap.getHistogram());
        
    	thresholdView.setThreshold(cccontext.getThreshold());
        thresholdView.setSourceBitmap(ColonyCountHeap.getLumaBitmap());
    	thresholdView.setLumaMap(ColonyCountHeap.getLumaMap());
    }
    
    void removeHeapReferences()
    {
        histogramView.setHistogram(null);
        thresholdView.setSourceBitmap(null);
    	thresholdView.setLumaMap(null);
    	System.gc();
    }
    
//    public synchronized void stopWorkerThread(){
//    	  if(workerThread != null){
//    	    Thread moribund = workerThread;
//    	    workerThread = null;
//    	    moribund.interrupt();
//    	  }
//    	}
    
    void startWorkerThread()
    {
		if(workerThread == null)
		{
			final Activity activity = this;
        	removeHeapReferences();
			dialog = ProgressDialog.show(ThresholdActivity.this,"Processing", "Creating Region Map", true, true, this);
			workerThread = new Thread() {
	            public void run() {
	            	
	            	
	            	//cccontext.identifyRegions();
	            	
	            	mHandler.post(mWorkerThreadResults);
	            }
			};
			workerThread.start();
		};
    }
	
    final Runnable mWorkerThreadResults = new Runnable() {
        public void run() {
    		dialog.dismiss();
    		workerThread = null;
    		
			Intent myIntent = new Intent(ThresholdActivity.this, ROIActivity.class);
            startActivity(myIntent);
        }
    };	
    
	@Override
	public void onThresholdChanged(int threshold) {
		cccontext.setThreshold(threshold);
		thresholdView.setThreshold(cccontext.getThreshold());
	}

	@Override
	public void onCancel(DialogInterface arg0) {
		
		// only allow cancel if workerthread not running
		if(workerThread == null)
		{
			finish();
		}
	}
}
